package com.teach.onclass.teacher;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

import com.teach.JobApplication;
import com.teach.MainActivity;
import com.teach.R;
import com.teach.ShowClasssActivity;

public class TeacherUpClassWorkActivity extends Activity {
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		bindWorkList();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	class Answerquestion implements OnClickListener{

		@Override
		public void onClick(View v) {
			
		}
		
	}
	
	private void bindWorkList() {
		LinearLayout layout = new LinearLayout(this);
		layout.setOrientation(LinearLayout.VERTICAL);
		JSONObject jo;
		if (MainActivity.isTeacher)
			jo = JobApplication.getJobByGroupIdAndType(
					ShowClasssActivity.currentGroupId, 1);
		else
			jo = JobApplication.getJobByGroupIdAndType(MainActivity.groupId, 1);
		JSONArray ja;
		try {
			ja = jo.getJSONArray("result");
			for (int i = 0; i < ja.length(); i++) {
				JSONObject object = ja.getJSONObject(i);
				String submitTime = object.getString("submit_time");
				String name = object.getString("name");
				int jobId = object.getInt("job_id");

				Button button = new Button(this);
				button.setId(jobId);
				button.setText(submitTime + name);
				button.setOnClickListener(new DetailListener());
				layout.addView(button);
			}

			setContentView(layout);
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
	class DetailListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			int jobId = v.getId();
			TeacherUpClassWorkDetailActivity.jobId = jobId;
			Intent intent = new Intent();
			intent.setClass(TeacherUpClassWorkActivity.this,
					TeacherUpClassWorkDetailActivity.class);
			startActivity(intent);
		}

	}
}
